<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateUserShopGoodsGroupGroupCategoryGoodsCategoryBrandBrandShopGradeShopManagementShopApplyTables extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->string('mobile', 25)->comment('手机号码');
            $table->string('nickname', 50)->comment('昵称');
            $table->string('password', 100)->nullable()->comment('密码');
            $table->unsignedTinyInteger('sex')->default(0)->comment('性别|["未知","男","女"]');
            $table->date('birthday')->nullable()->comment('生日');
            $table->unsignedDecimal('consumption_amount', 10)->default(0)->comment('累计消费');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态|{"1":"正常","2":"禁用","3":"冻结"}');
            $table->timestampsTz();
            $table->softDeletesTz();
            $table->comment('用户表');
        });

        Schema::create('payer', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->string('name',50)->comment('姓名');
            $table->string('id_card', 25)->comment('身份证号码');
            $table->string('mobile')->comment('手机号码');
            $table->string('front_img')->nullable()->comment('身份证正面');
            $table->string('back_img')->nullable()->comment('身份证反面');
            $table->string('hand_front_img')->nullable()->comment('手持身份证正面');
            $table->string('hand_back_img')->nullable()->comment('手持身份证反面');
            $table->unsignedTinyInteger('is_default')->default(0)->comment('是否默认|["否","是"]');
            $table->timestampsTz();
            $table->softDeletesTz();
            $table->comment('实名认证信息');
        });

        Schema::create('shop', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 100)->comment('店铺名称');
            $table->string('prov', 50)->nullable()->comment('省');
            $table->string('city', 50)->nullable()->comment('市');
            $table->string('area', 50)->nullable()->comment('区');
            $table->string('address', 100)->nullable()->comment('详细地址');
            $table->unsignedTinyInteger('status')->default(1)->comment('店铺状态|{"1":"审核中","2":"开启","3":"关闭"}');
            $table->string('close_reason')->nullable()->comment('关闭原因');
            $table->date('end_date')->nullable()->comment('店铺截止日期');
            $table->string('logo')->nullable()->comment('店铺LOGO');
            $table->string('seo_keyword')->nullable()->comment('店铺SEO关键字');
            $table->string('seo_desc')->nullable()->comment('店铺SEO描述');
            $table->string('mobile', 25)->nullable()->comment('店铺手机号码');
            $table->unsignedInteger('sale')->default(0)->comment('店铺销量');
            $table->unsignedInteger('inventory_alert')->default(0)->comment('库存预警');
            $table->string('service_mobile', 25)->nullable()->comment('服务电话,用于接收通知信息');
            $table->string('notice')->nullable()->comment('店铺公告');
            $table->string('order_prefix', 10)->nullable()->comment('订单号码前缀');
            $table->string('default_user_head')->nullable()->comment('用户默认头像');
            $table->string('pay_customs_area', 30)->nullable()->comment('支付报关');
            $table->timestampsTz();
            $table->softDeletesTz();
            $table->comment('店铺');
        });

        Schema::create('shop_management', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nickname', 50)->comment('昵称');
            $table->string('username', 20)->comment('用户名');
            $table->string('password', 100)->comment('密码');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->unsignedTinyInteger('is_admin')->comment('是否为管理员|["否","是"]');
            $table->dateTimeTz('last_at')->nullable()->comment('上次登录时间');
            $table->unsignedInteger('last_ip')->nullable()->comment('上次登录IP');
            $table->unsignedTinyInteger('status')->default(1)->comment('是否启用|["否","是"]');
            $table->unsignedTinyInteger('is_new')->default(1)->comment('新手向导|["否","是"]');
            $table->timestampsTz();
            $table->softDeletesTz();
            $table->comment('店铺管理员表');
            $table->foreign('user_id')->references('id')->on('user')->onDelete('cascade');
            $table->foreign('shop_id')->references('id')->on('shop')->onDelete('cascade');
        });

        Schema::create('good', function (Blueprint $table){
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->string('product_id', 80)->comment('商品标识');
            $table->string('no', 80)->nullable()->comment('商品编码');
            $table->string('barcode')->nullable()->comment('规格条码');
            $table->string('name', 50)->comment('商品名称');
            $table->string('title', 100)->nullable()->comment('商品标题');
            $table->string('brand', 30)->nullable()->comment('商品品牌');
            $table->unsignedDecimal('price', 10)->default(0)->comment('单价');
            $table->longText('content')->nullable()->comment('商品详情');
            $table->string('image')->nullable()->comment('商品封面');
            $table->unsignedTinyInteger('status')->default(0)->comment('是否上架|["否","是"]');
            $table->unsignedSmallInteger('sort')->default(0)->comment('排序');
            $table->unsignedBigInteger('sales_num')->default(0)->comment('销量');
            $table->unsignedSmallInteger('virtual_sales_num')->default(0)->comment('虚拟销量');
            $table->string('upc', 50)->nullable()->comment('UPC');
            $table->unsignedTinyInteger('audit_status')->default(1)->comment('审核状态|{"1":"待审核","2":"审核通过","3":"审核失败","4":"违规下架"}');
            $table->string('violation_reason')->nullable()->comment('违规原因');
            $table->string('video')->nullable()->comment('视频');
            $table->dateTimeTz('enable_at')->nullable()->comment('上架时间');
            $table->unsignedTinyInteger('spec_type')->default(0)->comment('是否多规格|["否","是"]');
            $table->timestampsTz();
            $table->softDeletesTz();
            $table->comment('商品表');
            $table->foreign('shop_id')->references('id')->on('shop')->onDelete('cascade');
        });

        Schema::create('good_attr', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('good_id')->comment('商品ID');
            $table->string('sku_id', 60)->comment('商品属性ID');
            $table->string('sku_code', 60)->nullable()->comment('规格编码');
            $table->unsignedDecimal('price')->default(0)->comment('单价');
            $table->string('image')->comment('规格图片');
            $table->string('barcode', 50)->nullable()->comment('规格条码');
            $table->string('attr')->nullable()->comment('商品规格');
            $table->timestampsTz();
            $table->comment('商品规格');
        });

        Schema::create('app', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->string('app_id', 50)->comment('应用ID');
            $table->string('app_secret', 100)->comment('应用密钥');
            $table->string('app_name', 30)->comment('应用名称');
            $table->string('payment_notify')->nullable()->comment('支付回调路径');
            $table->unsignedTinyInteger('status')->default(1)->comment('是否启用|["否","是"]');
            $table->timestampsTz();
            $table->comment('应用表');
            $table->foreign('shop_id')->references('id')->on('shop')->onDelete('cascade');
        });

        Schema::create('app_notify', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('no', 60)->comment('订单号码');
            $table->string('url')->comment('回调地址');
            $table->string('request_type')->comment('请求类型');
            $table->json('request_info')->nullable()->comment('请求详情');
            $table->json('response_info')->nullable()->comment('响应内容');
            $table->unsignedTinyInteger('status')->default(0)->comment('请求状态|["未请求","请求成功","请求失败"]');
            $table->unsignedTinyInteger('notify_num')->default(0)->comment('请求回调次数');
            $table->string('scene', 20)->nullable()->comment('场景');
            $table->timestampsTz();
            $table->comment('应用回调表');
        });

        Schema::create('order', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->unsignedBigInteger('app_id')->comment('应用ID');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->string('no', 50)->comment('订单号码');
            $table->string('merchant_no', 50)->comment('商户订单号');
            $table->string('consignee', 50)->comment('收货人姓名');
            $table->string('address')->comment('收货人详细地址');
            $table->string('mobile', 25)->comment('收货人手机号码');
            $table->string('prov', 50)->comment('收货省');
            $table->string('city', 50)->comment('收货市');
            $table->string('area', 50)->comment('收货区');
            $table->dateTimeTz('shipping_at')->nullable()->comment('发货时间');
            $table->unsignedDecimal('paid_amount')->default(0)->comment('实际支付金额');
            $table->unsignedDecimal('postage_amount')->default(0)->comment('邮费');
            $table->unsignedDecimal('discount_amount')->default(0)->comment('优惠金额');
            $table->dateTimeTz('confirm_at')->nullable()->comment('确认收货时间');
            $table->dateTimeTz('paid_at')->nullable()->comment('支付时间');
            $table->string('paid_no', 60)->nullable()->comment('支付单号');
            $table->dateTimeTz('refund_at')->nullable()->comment('退款时间');
            $table->dateTimeTz('complete_at')->nullable()->comment('完成时间');
            $table->string('user_note')->nullable()->comment('用户留言');
            $table->unsignedTinyInteger('status')->default(1)->comment('订单状态|{"1":"待支付","2":"待发货","3":"待收货","4":"已收货","5":"已完成","6":"已取消","7":"已关闭"}');
            $table->unsignedTinyInteger('pay_status')->default(1)->comment('支付状态|{"1":"待支付","2":"部分支付","3":"已支付","4":"部分退款","5":"已退款","6":"申请退款中","7":"拒绝退款"}');
            $table->string('payer_name', 30)->nullable()->comment('支付人姓名');
            $table->string('payer_no', 30)->nullable()->comment('支付人身份证号码');
            $table->string('payer_mobile', 25)->nullable()->comment('支付人手机号码');
            $table->json('payer')->nullable()->comment('实名信息');
            $table->timestampsTz();
            $table->comment('订单表');
        });

        Schema::create('invoice', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('order_id')->comment('订单号');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->string('consignee', 50)->comment('收货人');
            $table->string('zipcode', 10)->nullable()->comment('邮箱');
            $table->string('mobile', 25)->nullable()->comment('手机号码');
            $table->string('prov', 20)->comment('省');
            $table->string('city', 20)->comment('市');
            $table->string('area', 20)->comment('区');
            $table->string('address')->comment('详细地址');
            $table->string('logistics_code')->nullable()->comment('物流编码');
            $table->string('logistics_name')->nullable()->comment('物流公司');
            $table->unsignedDecimal('postage_amount', 6)->default(0)->comment('邮费');
            $table->string('logistics_no', 50)->comment('物流单号');
            $table->string('note')->nullable()->comment('备注');
            $table->dateTimeTz('best_at')->nullable()->comment('友好收货时间');
            $table->unsignedTinyInteger('type')->default(1)->comment('发货方式|{"1":"自填快递","2":"在线预约","3":"电子面单","4":"无需物流"}');
            $table->timestampsTz();
            $table->comment('发货单');
        });

        Schema::create('order_good', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shop_id')->comment('店铺ID');
            $table->unsignedBigInteger('order_id')->comment('订单ID');
            $table->json('good')->nullable()->comment('商品内容');
            $table->unsignedDecimal('paid_price')->default(0)->comment('付款单价');
            $table->unsignedDecimal('preferential_price')->default(0)->comment('商品优惠价');
            $table->unsignedSmallInteger('num')->comment('商品数量');
            $table->string('sku')->nullable()->comment('商品SKU');
            $table->string('good_no', 50)->nullable()->comment('商品编码');
            $table->string('product_id', 50)->comment('商品标识');
            $table->string('barcode', 50)->nullable()->comment('商品条码');
            $table->unsignedTinyInteger('shipment_status')->default(1)->comment('发货状态|{"1":"未发货","2":"已发货","3":"已换货","4":"已退货"}');
            $table->unsignedBigInteger('invoice_id')->nullable()->comment('发货单');
            $table->string('snapshot')->nullable()->comment('商品快照');
            $table->string('product_link')->nullable()->comment('商品链接');
            $table->timestampsTz();
            $table->comment('订单商品表');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('order_good');
        Schema::dropIfExists('invoice');
        Schema::dropIfExists('order');
        Schema::dropIfExists('app_notify');
        Schema::dropIfExists('app');
        Schema::dropIfExists('good_attr');
        Schema::dropIfExists('good');
        Schema::dropIfExists('shop_management');
        Schema::dropIfExists('shop');
        Schema::dropIfExists('payer');
        Schema::dropIfExists('user');
    }
}
